from player import *
from level import *
from constants import *
from os import listdir, sep

class Game:
	"""Allows to move the character and interact with the level's objects.
	-- Class variables --
	isRunning  : boolean : Condition for the main loop to run.
	player     : Player  : Contains current player information.
	level      : Level   : Contains current level information.
	canSave    : boolean : Condition for the user to be allowed to save (False for brand new Game).
	window     : Pygame  : Main window of the game.
	savefile   : string  : Name of the file where the current game data will be saved.
	-- Class public methods --
	run        : None    : Main loop of the game.
	load       : None    : Converts data from file into the Game object.
	save       : None    : Converts data from the Game object into a file.
	"""
	def __init__(self, player, levelfile, savefile, window):
		self.isRunning = True
		self.player = player
		self.level = Level(levelfile)
		self.canSave = False
		self.window = window
		self.savefile = savefile
		self.levelfile = levelfile

	def run(self):
		"""Runs the game main loop.
		-- Inputs --
		None
		-- Outputs --
		None
		"""
		self.canSave = True
		while (self.isRunning):
			pygame.time.Clock().tick(Delay)
			self.level.displayBackground(self.window)
			surroundings = self.level.getSurroundings(self.player.x, self.player.y)
			self.level.diplaySurroundings(self.window, surroundings)
			self.window.blit(self.player.sprite, (X_player,Y_player))
			pygame.display.flip()
			for event in pygame.event.get():
				# Quitter le jeu
				if ((event.type == QUIT) or (event.type == KEYDOWN and event.key == K_ESCAPE)):
					self.isRunning = False
				elif (event.type == KEYDOWN and event.key == K_DOWN):
					if (self.level.access[self.player.x][self.player.y+1]):
						self.player.y += 1
				elif (event.type == KEYDOWN and event.key == K_UP):
					if (self.level.access[self.player.x][self.player.y-1]):
						self.player.y -= 1
				elif (event.type == KEYDOWN and event.key == K_LEFT):
					if (self.level.access[self.player.x-1][self.player.y]):
						self.player.x -= 1
				elif (event.type == KEYDOWN and event.key == K_RIGHT):
					if (self.level.access[self.player.x+1][self.player.y]):
						self.player.x += 1
				elif (event.type == KEYDOWN and event.key == K_s):
					if (self.canSave):
						self.saveGame()
		self.canSave = False
		return

	def saveGame(self):
		"""Exports the data from the Game object to the output file.
		-- Inputs --
		filename   : String    : Name of the file where the data will be exported.
		-- Outputs --
		None
		"""
		f = open('%s/%s'%(CharsDirectory,self.savefile), 'w')
		f.write("%s\n%s\n"%(FileNameSection, self.player.name))
		f.write("%s\n%s\n"%(FileLevelSection, self.levelfile.split(os.sep)[1]))
		f.write("%s\n%s\n"%(FilePositionSection, "%i\t%i"%(self.player.x, self.player.y)))
		f.close()
		return

	def goToLevel(self, filename, position):
		"""Called when the character enters a passage to another level. Loads the new level and sets the character's new position.
		-- Inputs --
		filename   : String          : Name of the file from where the data will be imported.
		position   : tuple(int, int) : Position of the player in the new level.
		-- Outputs --
		None
		"""
		# Loads the new level in the Level object.
		# Sets the new position in the Player object.
		pass
		return


